package com.ruoyi.resume.provider;

import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class TagSqlProvider {

    // 读取tag表的所有内容
    public String selectAllTags() {
        return new SQL() {{
            SELECT("*");
            FROM("tag");
        }}.toString();
    }

    // 根据ID读取tag表的内容
    public String selectTagById(Map<String, Object> params) {
        return new SQL() {{
            SELECT("*");
            FROM("tag");
            WHERE("id = #{id}");
        }}.toString();
    }

    // 根据ID更新tag表
    public String updateTag(Map<String, Object> params) {
        return new SQL() {{
            UPDATE("tag");
            SET("category_id = #{categoryId}");
            SET("tag_value = #{tagValue}");
            SET("created_at = NOW()");
            SET("tag_description = #{tagDescription}");
            SET("creator = #{creator}");
            WHERE("id = #{id}");
        }}.toString();
    }

    // 根据ID删除tag表的记录
    public String deleteTagById(Integer id) {
        return new SQL() {{
            DELETE_FROM("tag");
            WHERE("id = #{id}");
        }}.toString();
    }

    // 根据categoryId查询tag列表
    public String selectTagsByCategoryId(Map<String, Object> params) {
        return new SQL() {{
            SELECT("*");
            FROM("tag");
            WHERE("category_id = #{categoryId}");
        }}.toString();
    }


    /**
     * 查询所有分类及其对应的标签
     */
    public String selectAllCategoriesWithTags() {
        return "SELECT " +
                "c.id AS category_id, c.parent_id, c.name, c.level, " +
                "t.id AS tag_id, t.tag_value, t.created_at, t.tag_description, t.creator " +
                "FROM category c " +
                "LEFT JOIN tag t ON c.id = t.category_id " +
                "ORDER BY c.level ASC, c.id ASC";
    }
}
